package com.sofoscorp.android.gestcobranzamobile;

import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TBUKRS_BO{
	private String C_TABNAME = "TBUKRS";
	private ConexionDB go_conexDB;
	private Cursor gc_TBUKRS;
	private SQLiteDatabase go_db;
	
	private TBUKRS_Str gs_TBUKRS = null;
	private TBUKRS_Str[] gt_TBUKRS = null;
	String[] gt_campos = new String[]{"BUKRS", "BUTXT", "DIRECCION", "PAVAL","IMAGEN"};
	
	public TBUKRS_BO(){
		go_conexDB = new ConexionDB();
	}
	
	public TBUKRS_Str get_SingleByKey(String ivBUKRS){
		String lv_Where = "BUKRS = ?";
		String[] lt_WhereArgs;
		
		if(ivBUKRS.equals("") || ivBUKRS.equals(null)){
			gs_TBUKRS = null;
		}else {
			gc_TBUKRS = null;
			lt_WhereArgs = new String[]{ivBUKRS};
			
			//Pasar el Path de DB
			go_conexDB.serialization(go_conexDB.C_PATH_FROMSAP);

			//Verificar si existe conexion con la DB
			if (go_conexDB.checkConexDB()) {

				//Obtener la DB
				go_db = go_conexDB.getGo_DB();
				
				try {
					//Ejecutar Select a la DB
					gc_TBUKRS = go_db.query(C_TABNAME, gt_campos, lv_Where, lt_WhereArgs, null, null, null, null);
					if (gc_TBUKRS.moveToFirst()) {
						gs_TBUKRS = new TBUKRS_Str(	gc_TBUKRS.getString(0), 
													gc_TBUKRS.getString(1), 
													gc_TBUKRS.getString(2), 
													gc_TBUKRS.getString(3),
													gc_TBUKRS.getString(4));
					}else{
						gs_TBUKRS = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}else{
				gs_TBUKRS = null;
			}
		}

		go_db.close();
		gc_TBUKRS.close();
		return gs_TBUKRS;
	}
	
	public TBUKRS_Str[] get_AllTBUKRS(){
		ArrayList<TBUKRS_Str> lt_List = new ArrayList<TBUKRS_Str>();
		gc_TBUKRS = null;
		
		//Pasar el Path de DB
		go_conexDB.serialization(go_conexDB.C_PATH_FROMSAP);
		
		//Verificar si existe conexion con la DB
		if (go_conexDB.checkConexDB()) {
			
			//Obtener la DB
			go_db = go_conexDB.getGo_DB();
			try {
				//Ejecutar Select a la DB
				gc_TBUKRS = go_db.query(C_TABNAME, gt_campos, null, null, null, null, null, null);
				if (gc_TBUKRS.moveToFirst()) {
					//Recorrer el cursor
					do {
						lt_List.add(new TBUKRS_Str(	gc_TBUKRS.getString(0), 
													gc_TBUKRS.getString(1), 
													gc_TBUKRS.getString(2), 
													gc_TBUKRS.getString(3),
													gc_TBUKRS.getString(4)));
					} while (gc_TBUKRS.moveToNext());
					//Pasar los datos del Lista al Arreglo de objetos
					gt_TBUKRS = lt_List.toArray(new TBUKRS_Str[0]);
				}else{
					gt_TBUKRS = null;
					lt_List = null;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}else{
			gt_TBUKRS = null;
			lt_List = null;
		}
		
		go_db.close();
		gc_TBUKRS.close();
		return gt_TBUKRS;
	}
}
